package com.yxboot.modules.call.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxboot.enums.UpdateType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yxboot.modules.call.mapper.XNoQsMapper;
import com.yxboot.modules.call.entity.XNoQs;
import com.yxboot.common.pagination.PageRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * X号码表 业务实现类
 * @author exchenming
 * @date 2025-08-20
 */
@Service
@Slf4j
public class XNoQsService extends ServiceImpl<XNoQsMapper, XNoQs> {
    @Autowired
    XNoQsMapper xNoQsMapper;
    public IPage<XNoQs> pageQuery(XNoQs xNoQs,PageRequest pageRequest) {
        QueryWrapper<XNoQs> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(xNoQs.getProvinceName())){
            wrapper.lambda().eq(XNoQs::getProvinceName,xNoQs.getProvinceName());
        }
        if(StringUtils.isNotBlank(xNoQs.getCityName())){
            wrapper.lambda().eq(XNoQs::getCityName,xNoQs.getCityName());
        }
        if(StringUtils.isNotBlank(xNoQs.getXno())){
            wrapper.lambda().like(XNoQs::getXno,"%"+xNoQs.getXno()+"%");
        }
        if(StringUtils.isNotBlank(xNoQs.getIsLock())){
            wrapper.lambda().eq(XNoQs::getIsLock,xNoQs.getIsLock());
        }
        if(StringUtils.isNotBlank(xNoQs.getIsCancel())){
            wrapper.lambda().eq(XNoQs::getIsCancel,xNoQs.getIsCancel());
        }
        if(StringUtils.isNotBlank(xNoQs.getIsUse())){
            wrapper.lambda().eq(XNoQs::getIsUse,xNoQs.getIsUse());
        }
        if(StringUtils.isNotBlank(xNoQs.getOpenStartTime())){
            wrapper.lambda().ge(XNoQs::getOpenTime,xNoQs.getOpenStartTime());
        }
        if(StringUtils.isNotBlank(xNoQs.getOpenEndTime())){
            wrapper.lambda().ge(XNoQs::getOpenTime,xNoQs.getOpenEndTime());
        }
        return page(pageRequest.convertToPage(), wrapper);
    }


    public void importExcel(MultipartFile file){
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String,Object>> readAll = reader.readAll();
            if(CollectionUtil.isNotEmpty(readAll)){
                List<XNoQs> batchXnos = new ArrayList<>();
                readAll.forEach(e->{
                    XNoQs xNoQs = new XNoQs();
                    xNoQs.setIsCancel("N");
                    xNoQs.setIsUse("Y");
                    xNoQs.setIsLock("N");
                    xNoQs.setXno(String.valueOf(e.get("号码")));
                    xNoQs.setProvinceName(String.valueOf(e.get("省份")));
                    xNoQs.setCityName(String.valueOf(e.get("本地网")));
                    xNoQs.setOpenTime(String.valueOf(e.get("开通时间")));
                    batchXnos.add(xNoQs);
                });
                super.saveBatch(batchXnos,100);
            }
            log.info(JSON.toJSONString(readAll));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public XNoQs getById(Long id){
        QueryWrapper<XNoQs> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(XNoQs::getId,id);
        return getOne(queryWrapper);
    }

    public List<XNoQs> getAllProvince(){
        QueryWrapper<XNoQs> queryWrapper = new QueryWrapper();
        queryWrapper.select("DISTINCT province_name","province_code");
        return list(queryWrapper);
    }
    public List<XNoQs> getCityByProvince(String provinceName){
        QueryWrapper<XNoQs> queryWrapper = new QueryWrapper();
        queryWrapper.select("DISTINCT city_name","city_code");
        if(StringUtils.isNotBlank(provinceName)) {
            queryWrapper.lambda().eq(XNoQs::getProvinceName,provinceName);
        }
        return list(queryWrapper);
    }

    public boolean batchUpdateIds(List<Long> ids, UpdateType typpe){
        UpdateWrapper<XNoQs> updateWrapper = new UpdateWrapper();
        updateWrapper.lambda().in(XNoQs::getId, ids);
        switch (typpe) {
            case USE:
                updateWrapper.lambda().set(XNoQs::getIsUse, "Y");
                break;
            case LOCK:
                updateWrapper.lambda().set(XNoQs::getIsLock, "Y");
                break;
            case CANCEL:
                updateWrapper.lambda().set(XNoQs::getIsCancel, "Y");
                break;
            default:
                log.error("不支持的更新类型");
                return Boolean.FALSE;
        }
        return super.update(updateWrapper);
    }
}